package com.utbm.lo54.dao.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import com.utbm.lo54.dao.CourseDAOInterface;
import com.utbm.lo54.entity.Course;

public class MysqlCourseDAO implements CourseDAOInterface {

	private DataSource dataSource;

	public MysqlCourseDAO(){
		try {
			Context namingContext = new InitialContext();
			dataSource = (DataSource) namingContext.lookup("java:comp/env/jdbc/testDatasource");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@Override
	public Course create(Course co) {
		Connection connection = null;
		try {
			connection = dataSource.getConnection();
			PreparedStatement stmt = (PreparedStatement) connection.prepareStatement("INSERT INTO COURSE.COURSE(CODE_COURSE,TITLE_COURSE) VALUES (?,?)");
			stmt.setString(1, co.getCode());
			stmt.setString(2, co.getTitle());
			stmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				connection.close();
			} catch (SQLException e) {

				e.printStackTrace();
			}
		}
		return co;
	}

	@Override
	public Course findByCode(String code) {
		Connection connection = null;
		Course co = new Course();
		try {
			connection = dataSource.getConnection();
			PreparedStatement stmt = (PreparedStatement) connection.prepareStatement("Select TITLE_COURSE FROM COURSE.COURSE WHERE CODE_COURSE=?");
			stmt.setString(1, code);

			ResultSet set = stmt.executeQuery();
			while(set.next()){
				co.setCode(code);
				co.setTitle(set.getString(1));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				connection.close();
			} catch (SQLException e) {

				e.printStackTrace();
			}
		}
		return co;
	}

	@Override
	public Course findByTitle(String title) {
		Connection connection = null;
		Course co = new Course();;
		try {
			connection = dataSource.getConnection();
			PreparedStatement stmt = (PreparedStatement) connection.prepareStatement("Select CODE_COURSE,TITLE_COURSE FROM COURSE.COURSE WHERE TITLE_COURSE=?");
			stmt.setString(1, title);

			ResultSet set = stmt.executeQuery();
			while(set.next()){
				co.setCode(set.getNString(1));
				co.setTitle(set.getNString(2));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				connection.close();
			} catch (SQLException e) {

				e.printStackTrace();
			}
		}
		return co;
	}

	@Override
	public void delete(Course co) {
		Connection connection = null;
		try {
			connection = dataSource.getConnection();
			Statement stmt = connection.createStatement();
			stmt.executeQuery("DELETE FROM COURSE.COURSE WHERE CODE_COURSE =" + co.getCode());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}

	@Override
	public void update(Course co) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Set<Course> getAllCourse() {
		Connection connection = null;
		Set<Course> list = new HashSet<Course>();
		try {
			connection = dataSource.getConnection();
			PreparedStatement stmt = (PreparedStatement) connection.prepareStatement("Select CODE_COURSE,TITLE_COURSE FROM COURSE.COURSE");
			

			ResultSet set = stmt.executeQuery();
			while(set.next()){
				Course co = new Course();
				co.setCode(set.getString(1));
				co.setTitle(set.getString(2));
				list.add(co);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				connection.close();
			} catch (SQLException e) {

				e.printStackTrace();
			}
		}
		return list;
	}



}
